package
{
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemTrayIcon;
	import flash.display.Bitmap;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.display.NativeWindowResize;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import nid.Canvas;
	import nid.CornerPoint;
	
	/**
	 * ...
	 * @author Nidin P Vinayakan
	 */
	public class Chromless extends Sprite
	{
		[Embed(source="assets/iconDock.png")]
		public var IconDock:Class;
		
		private var btResizeBottomRight:CornerPoint;
		private var btResizeBottomLeft:CornerPoint;
		private var btResizeTopRight:CornerPoint;
		private var btResizeTopLeft:CornerPoint;
		private var canvas:Canvas;
		
		public function Chromless() 
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			init();
		}
		private function init():void {
			configUI();
			registerMovementHandler();
			setDock();
		}
		private function configUI():void {
			canvas = new Canvas();
			addChild(canvas);
			
			btResizeBottomRight = new CornerPoint();
			btResizeBottomLeft	= new CornerPoint();
			btResizeTopRight	= new CornerPoint();
			btResizeTopLeft	 	= new CornerPoint();
			
			addChild(btResizeTopLeft);
			
			btResizeTopRight.x = stage.stageWidth - btResizeTopRight.width;
			addChild(btResizeTopRight);
			
			btResizeBottomLeft.y = stage.stageHeight - btResizeBottomLeft.height;
			addChild(btResizeBottomLeft);
			
			btResizeBottomRight.x = stage.stageWidth - btResizeBottomRight.width;
			btResizeBottomRight.y = stage.stageHeight - btResizeBottomRight.height;
			addChild(btResizeBottomRight);
		}
		public function registerMovementHandler():void {
			stage.addEventListener(Event.RESIZE,onResize)
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDrag);
			btResizeBottomRight.addEventListener(MouseEvent.MOUSE_DOWN, resizeWindow);
			btResizeBottomLeft.addEventListener(MouseEvent.MOUSE_DOWN, resizeWindow);
			btResizeTopRight.addEventListener(MouseEvent.MOUSE_DOWN, resizeWindow);
			btResizeTopLeft.addEventListener(MouseEvent.MOUSE_DOWN, resizeWindow);
		}
		
		private function onResize(e:Event):void 
		{
			canvas.width 			= stage.stageWidth;
			canvas.height 			= stage.stageHeight;
			btResizeTopRight.x 		= stage.stageWidth - btResizeTopRight.width;			
			btResizeBottomLeft.y	= stage.stageHeight - btResizeBottomLeft.height;
			btResizeBottomRight.x 	= stage.stageWidth - btResizeBottomRight.width;
			btResizeBottomRight.y 	= stage.stageHeight - btResizeBottomRight.height;
		}

		public function mouseDrag(e:MouseEvent):void {
			e.updateAfterEvent();
			if (e.target == e.currentTarget) stage.nativeWindow.startMove();
		}

		public function resizeWindow(e:MouseEvent = null):void {
			if (e.target == e.currentTarget) {
				var corner:String;
				switch(e.currentTarget){
					case btResizeBottomRight:
						corner = NativeWindowResize.BOTTOM_RIGHT;
						break;
					case btResizeBottomLeft:
						corner = NativeWindowResize.BOTTOM_LEFT;
						break;
					case btResizeTopRight:
						corner = NativeWindowResize.TOP_RIGHT;
						break;
					case btResizeTopLeft:
						corner = NativeWindowResize.TOP_LEFT;
						break;
				}
				stage.nativeWindow.startResize(corner);
			}
		}

		public function setDock():void {
			if (NativeApplication.supportsSystemTrayIcon){
				dockProperties();
				SystemTrayIcon(NativeApplication.nativeApplication.icon).menu = dockMenu();
			}
		}

		private function dockProperties():void{
			SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = "chromeless window";
			SystemTrayIcon(NativeApplication.nativeApplication.icon).addEventListener(MouseEvent.CLICK, undock);
		}

		private function dockMenu():NativeMenu {
			var menu:NativeMenu = new NativeMenu();
			var open:NativeMenuItem = new NativeMenuItem("Open");
			var close:NativeMenuItem = new NativeMenuItem("Close");
			open.addEventListener(Event.SELECT, undock);
			close.addEventListener(Event.SELECT, closeWindow);
			menu.addItem(open);
			menu.addItem(new NativeMenuItem("",true));
			menu.addItem(close);
			return menu;
		}

		public function dock():void {
			stage.nativeWindow.visible = false;
			NativeApplication.nativeApplication.icon.bitmaps = [new IconDock() as Bitmap];
		}

		public function undock(evt:Event):void {
			stage.nativeWindow.visible = true;
			stage.nativeWindow.orderToFront();
			NativeApplication.nativeApplication .icon.bitmaps = [];
		}

		public function closeWindow(e:Event = null):void {
			stage.nativeWindow.close();
		}

		public function setFullscreen():void {
			var state:String = (stage.displayState == StageDisplayState.FULL_SCREEN) ? StageDisplayState.NORMAL : StageDisplayState.FULL_SCREEN; 
			stage.displayState = state;
		}
	}

}